package com.yenlien.traceyun.business.dao;

import com.yenlien.traceyun.business.entity.BaseSoilEquipment;
import com.yenlien.traceyun.webase.dao.BaseJpaDao;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-11-24
 * @description:
 **/
public interface BaseSoilEquipmentDao extends BaseJpaDao<BaseSoilEquipment> {

    List<BaseSoilEquipment> findByCompanyIdAndAppId(String companyId, String appId);

    List<BaseSoilEquipment> findByEquipmentId(String equipmentId);

    @Query(value = "SELECT massif_id FROM t_base_soil_equipment WHERE equipment_id = ?1", nativeQuery = true)
    String getMassifIdByEquId(String equipmentId);

    @Transactional
    @Modifying
    @Query("update BaseSoilEquipment bse set bse.type=?1 , bse.equipmentName=?2 where bse.equipmentId=?3")
    public void updateByEquipmentId(String type, String name, String equipmentId);

    public int countByEquipmentId(String equipmentId);

    public List<BaseSoilEquipment> findByBaseSoilId(String baseSoildId);

    // 每个实体要都要有一个对应的XXXRepository来执行CRUD操作的 否则就报 ConverterNotFoundException: No converter found capable of converting from type xx
//    @Query(value = "SELECT * FROM t_base_soil_equipment tbse WHERE EXISTS (SELECT id FROM t_equipment te WHERE te.id = tbse.equipment_id)", nativeQuery = true)
    @Query(value = "SELECT * FROM t_base_soil_equipment tbse WHERE equipment_id IN (?1)", nativeQuery = true)
    List<BaseSoilEquipment> getEquipmentBaseSoil(List<String> equipmentId);

    @Query(value = "SELECT * FROM t_base_soil_equipment tbse WHERE equipment_id = ?1", nativeQuery = true)
    BaseSoilEquipment getBaseIdByEquId(String equId);

    @Query(value = "SELECT * FROM t_base_soil_equipment tbse WHERE massif_id = ?1", nativeQuery = true)
    List<BaseSoilEquipment> getEquipmentByMassifId(String massifId);

    @Query(value = "SELECT * FROM t_base_soil_equipment tbse WHERE massif_id = ?1 AND type = ?2", nativeQuery = true)
    List<BaseSoilEquipment> getEquipmentByMassifIdType(String massifId, String type);
}
